package com.itfreer.ui.photoview;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.itfreer.ui.R;
import com.itfreer.ui.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

/**
 * 单个照片查看器
 */
public class PhotoActivity extends BaseActivity {

    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_img);
        ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
        final TextView tv_num = (TextView) findViewById(R.id.tv_num);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        PhotoInfo info = (PhotoInfo)bundle.getSerializable("info");

        for(String item : info.getFiles()) {
            if(item==null || item.equals("")) continue;

            Fragment fragment1 = PhotoFragment.newInstance(item);
            fragmentList.add(fragment1);
        }

        tv_num.setText(1 + "/" + fragmentList.size());
        PhotoFragmentAdapter adapter = new PhotoFragmentAdapter(getSupportFragmentManager(), fragmentList);
        viewpager.setAdapter(adapter);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_num.setText(position + 1 + "/" + fragmentList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewpager.setCurrentItem(info.getPosition());
    }

    public void onBack(View view) {
        finish();
    }
}
